﻿namespace AjLogo
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    public class BindingEnvironment
    {
        private Dictionary<string, object> values = new Dictionary<string, object>();

        private BindingEnvironment parent;

        public BindingEnvironment(Machine machine)
        {
            this.Machine = machine;
        }

        public BindingEnvironment(BindingEnvironment parent)
        {
            this.Machine = parent.Machine;
            this.parent = parent;
        }

        internal Machine Machine { get; private set; }

        public object GetValue(string name)
        {
            string key = Machine.NormalizeKey(name);

            if (this.values.ContainsKey(key))
            {
                return this.values[key];
            }

            if (this.parent != null)
            {
                return this.parent.GetValue(name);
            }

            return null;
        }

        public void SetValue(string name, object value)
        {
            string key = Machine.NormalizeKey(name);

            this.values[key] = value;
        }
    }
}
